package com.example.form.service.impl;

import com.example.form.common.AppResult;
import com.example.form.common.ResultCode;
import com.example.form.config.AppConfig;
import com.example.form.dao.BoardMapper;
import com.example.form.exception.ApplicationException;
import com.example.form.model.Board;
import com.example.form.service.IBoradService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j//打印日志的
@Service//类注解,交由spring管理
public class BoardServiceImpl implements IBoradService {
    @Resource
    private BoardMapper boardMapper;//注入操作数据库的操作类
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if (num<=0){//参数查到传递的查询页数有误
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());//打印参数校验失败的日志
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));//抛出参数校验失败异常
        }
        //参数正确,调用dao层的代码
        List<Board> boards=boardMapper.selectByNum(num);//获取查询到的集合
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空判断
        if (id==null||id<0){//代表传入的板块id有问题
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());//打印更新帖子数量失败
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //根据传入的板块id.来查询对应的板块信息
        Board board=boardMapper.selectByPrimaryKey(id);//获取对应的板块的信息
        //校验得到的板块的信息
        if (board==null){//代表当前的板块id,不存在数据库中,失败
            // 打印⽇志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //现在可以更新当前板块的帖子数量了
        Board updateBoard=new Board();//要修改的话,我们是定义一个新的板块对象,id与我们刚刚查询到的信息一样(因为写的SQL中,传入的就是对象,按照属性是否为空赋值的)
        updateBoard.setId(board.getId());//id为我们刚刚通过传入的id查询到的board对象的id
        updateBoard.setArticleCount(board.getArticleCount()+1);//设置新的板块的帖子数量为原来的帖子数量+1
        int row =boardMapper.updateByPrimaryKeySelective(updateBoard);//执行修改的sql
        //判断受影响的行数
        if (row!=1){
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        // 参数校验
        if (id == null || id <= 0) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board result = boardMapper.selectByPrimaryKey(id);
        return result;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空判断
        if (id==null||id<0){//代表传入的板块id有问题
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());//打印更新帖子数量失败
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //根据传入的板块id.来查询对应的板块信息
        Board board=boardMapper.selectByPrimaryKey(id);//获取对应的板块的信息
        //校验得到的板块的信息
        if (board==null){//代表当前的板块id,不存在数据库中,失败
            // 打印⽇志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //构造更新对象
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());//设置板块的id
        updateBoard.setArticleCount(board.getArticleCount()-1);//在原来的帖子数量上减一
        //判断减一后是否小于0,
        if (updateBoard.getArticleCount()<0){//小于0是不合理的,把它设置为0
            updateBoard.setArticleCount(0);//设置为0
        }
        //调用dao执行动态修改操作
        int row=boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断执行的结果
        if (row!=1){
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
